/* 
 * JUGJobs -- A Jobs Posting Application for Java Users Groups and Other Groups
 * $Id: JobBean.java,v 1.7 2007/01/25 00:54:05 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the JUGJobs project.
 *
 * The Initial Developers of the Original Code are the members of the Triangle
 * Java User's Group in the RTP area of North Carolina.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developers. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.jugjobs.form;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.jugjobs.botrepellent.BotRepellent;
import org.jugjobs.util.LegalHtmlScanner;
import org.jugjobs.util.NonStrippingHtmlScanner;

/**
 * @author BigLee
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JobBean extends ActionForm {
 
  //the rest of the variables are job data
  private String job_number = "-1";
  private String job_title;
  private String employer_name;
  private String employer_url;
  private String job_desc;
  private String contact_name;
  private String contact_info;
  private String posting_password;
  private String original_password;
  private String admin_contact_info;
  private String last_edit_date;
  private String captcha;
  private int    ageInDays;  public int getAgeInDays() {return ageInDays;}  
  private int    arrayPosition; public int getArrayPosition() { return arrayPosition; } public void setArrayPosition(int value) {arrayPosition = value;}
  private Date   lastEditTimestamp;
  
  private static DateFormat dateFormat = 
                     DateFormat.getDateInstance(DateFormat.SHORT);

    public String getJob_number() {
      return (job_number);
    }

    public void setJob_number(String s) {
        job_number = s;
    }

    public String getJob_title() {
      return (job_title);
    }

    public void setJob_title(String s) {
        job_title = s;
    }
    
    public String getEmployer_name() {
      return (employer_name);
    }

    public void setEmployer_name(String s) {
        employer_name = s;
    }
    
    public String getEmployer_url() {
      return (employer_url);
    }

    public void setEmployer_url(String s) {
        employer_url = s;
    }
    
    public String getJob_desc() {
      return (job_desc);
    }
    
    public String getJob_summary() {
    	NonStrippingHtmlScanner scanner = new NonStrippingHtmlScanner(getJob_desc());
    	String retval = "Error";
		try {
			retval = scanner.summarize();
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return retval;
    }

    public void setJob_desc(String s) {
        job_desc = s;
    }
    
    public String getContact_name() {
      return (contact_name);
    }

    public void setContact_name(String s) {
        contact_name = s;
    }
    
    public String getContact_info() {
      return (contact_info);
    }

    public void setContact_info(String s) {
        contact_info = s;
    }
    
    public String getPosting_password() {
      return (posting_password);
    }

    public void setPosting_password(String s) {
        posting_password = s;
    }

    public String getOriginal_password() {
      return (original_password);
    }

    public void setOriginal_password(String s) {
        original_password = s;
    }

    public String getAdmin_contact_info() {
      return (admin_contact_info);
    }

    public void setAdmin_contact_info(String s) {
        admin_contact_info = s;
    }
    
    public String getLast_edit_date() {
      return (last_edit_date);
    }

    public void setLast_edit_date(Date d) {
    	if(d==null) {
    		last_edit_date = "";
    		lastEditTimestamp = new Date(0);
    	}
    	else {
    		lastEditTimestamp = d;
            last_edit_date = dateFormat.format(d);
            try
            { 
              // get current time, and back it up to midnight.
              Calendar currentTime = new GregorianCalendar();
              currentTime.set(Calendar.HOUR_OF_DAY,  0);
              currentTime.set(Calendar.MINUTE,       0);
              currentTime.set(Calendar.MILLISECOND,  0);
              long currentDay = currentTime.getTimeInMillis();

              long lastEditTime = d.getTime();

              if (lastEditTime > currentDay) 
                { ageInDays = 0; }
              else
              { long millisOld = currentDay - lastEditTime;
                long daysOld = millisOld / (1000*60*60*24);
                ageInDays = (int) daysOld;
              }
            }
            catch (Exception e)
            { // couldn't calculate age in days for some reason.
              ageInDays = -1;
            }
    	}
    	
    }
    
	public Date getLastEditTimestamp() {
		return lastEditTimestamp;
	}
	
	public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {

		ActionErrors errors = new ActionErrors();
		HttpSession session = request.getSession();
		BotRepellent br = new BotRepellent();
		
		// check for required fields
		if(getEmployer_name()==null || getEmployer_name().trim().length()<1) {
			errors.add("coname",new ActionMessage("jugjobs.jobs.validation.required"));
		}
		if(getContact_name()==null || getContact_name().trim().length()<1) {
			errors.add("contactname",new ActionMessage("jugjobs.jobs.validation.required"));
		}
		if(getContact_info()==null || getContact_info().trim().length()<1) {
			errors.add("contact",new ActionMessage("jugjobs.jobs.validation.required"));
		}
		if(getJob_title()==null || getJob_title().trim().length()<1) {
			errors.add("jobtitle",new ActionMessage("jugjobs.jobs.validation.required"));
		}
		if(getPosting_password()==null || getPosting_password().trim().length()<1) {
			errors.add("password",new ActionMessage("jugjobs.jobs.validation.required"));
		}
		if(getAdmin_contact_info()==null || getAdmin_contact_info().trim().length()<1) {
			errors.add("admincontact",new ActionMessage("jugjobs.jobs.validation.required"));
		}
		if(getJob_desc()==null || getJob_desc().trim().length()<1) {
			errors.add("jobdesc",new ActionMessage("jugjobs.jobs.validation.required"));
		}
		if(getCaptcha()==null 
		|| getCaptcha().trim().length()<4 
		|| !br.validateDigitImageChallenge(session,getCaptcha())) {
			errors.add("captcha",new ActionMessage("jugjobs.jobs.possible.bot"));
		}
		
		// scan job description for HTML errors.
		String desc = getJob_desc();
		if(desc!=null) {
			LegalHtmlScanner ls = new LegalHtmlScanner(getJob_desc());
			String problem = ls.scanForProblems();
			if(problem!=null) {
				errors.add("jobdesc",new ActionMessage("jugjobs.jobs.validation.badHTML",problem));
			}
		}
		
		setCaptcha("");
		br.resetChallenge(session);
		return errors;
	}
	/**
	 * @return the captcha
	 */
	public String getCaptcha() {
		return captcha;
	}
	/**
	 * @param captcha the captcha to set
	 */
	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

}
